<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.username" placeholder="用户账号" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态：">
         <el-select v-model="dataForm.status" clearable>
          <el-option value="0" label="禁用" key="0"></el-option>
          <el-option value="1" label="启用" key="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="测试：">
        <el-select v-model="dataForm.isTest" clearable>
          <el-option value="0" label="正式" key="0"></el-option>
          <el-option value="1" label="测试" key="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="setStatusMultiHandle(0)">批量禁用</el-button>
        <el-button type="primary" @click="setStatusMultiHandle(1)">批量启用</el-button>
        <el-button v-if="isAuth('biz:user:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="id">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="agentName"
        header-align="center"
        align="center"
        label="代理商">
      </el-table-column>
      <el-table-column
        prop="parentUserName1"
        header-align="center"
        align="center"
        label="上级用户">
      </el-table-column>
      <el-table-column
        prop="balance"
        header-align="center"
        align="center"
        label="余额">
      </el-table-column>
      <el-table-column
        prop="profit"
        header-align="center"
        align="center"
        label="盈亏总额">
      </el-table-column>
      <el-table-column
        prop="totalBet"
        header-align="center"
        align="center"
        label="下注总额">
      </el-table-column>
      <el-table-column
        prop="totalRecharge"
        header-align="center"
        align="center"
        label="充值总额">
      </el-table-column>
      <el-table-column
        prop="totalWithdraw"
        header-align="center"
        align="center"
        label="提现总额">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
         width="100"
        label="状态">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status == 0">禁用</el-tag>
          <el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="isTest"
        header-align="center"
        align="center"
         width="100"
        label="是否测试">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isTest == 0">正式</el-tag>
          <el-tag type="danger" v-if="scope.row.isTest == 1">测试</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="注册时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" v-if="scope.row.status == 1" @click="setStatusHandle(scope.row.userId, 0)">禁用</el-button>
          <el-button type="text" size="small" v-if="scope.row.status == 0" @click="setStatusHandle(scope.row.userId, 1)">启用</el-button>
          <el-button type="text" size="small" @click="showChangePassword(scope.row.userId)">重置密码</el-button>
          <el-button type="text" size="small" @click="showChangeBalance(scope.row.userId, scope.row.balance)">修改余额</el-button>
          <el-button type="text" size="small" @click="resetTrcAddressHandle(scope.row.userId, scope.row.trcAddress)">修改提现地址</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <el-dialog title="调整余额" :visible.sync="changeBalanceVisible">
      <el-form :model="changBalanceForm" label-width="80px">
        <el-form-item label="当前余额" >
          <el-input v-model="changBalanceForm.userBalance" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="调整">
          <el-radio v-model="changBalanceForm.status" :label="0">增加</el-radio>
          <el-radio v-model="changBalanceForm.status" :label="1">减少</el-radio>
        </el-form-item>
        <el-form-item label="金额">
          <el-input v-model="changBalanceForm.balance" type="number" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="changeBalanceVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeBalanceHandle" :loading="changBalanceConfirmLoading">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="重置密码" :visible.sync="changePasswordVisible">
      <el-form :model="changePasswordForm" label-width="80px">
        <el-form-item label="登录密码" >
          <el-input v-model="changePasswordForm.password" placeholder="不填则不修改"></el-input>
        </el-form-item>
         <el-form-item label="交易密码" >
          <el-input v-model="changePasswordForm.exchangePassword" placeholder="不填则不修改"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="changePasswordVisible = false">取 消</el-button>
        <el-button type="primary" @click="changePasswordHandle" :loading="changePasswordConfirmLoading">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import AddOrUpdate from './user-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          username: '',
          status: '',
          isTest: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,

        //调账
        changeBalanceVisible: false,
        changBalanceConfirmLoading: false,
        changBalanceForm: {
          userId: null,
          userBalance: 0,
          balance: 0,
          status: 0
        },

        // 修改密码
        changePasswordVisible: false,
        changePasswordConfirmLoading: false,
        changePasswordForm: {
          userId: null,
          password:'',
          exchangePassword:''
        }
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/biz/user/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            ...this.dataForm
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
       // 显示重置密码
      showChangePassword (userId) {
        this.changePasswordForm.userId = userId
        this.changePasswordForm.password = ''
        this.changePasswordForm.exchangePassword = ''
        this.changePasswordVisible = true
      },
      // 重置密码
      changePasswordHandle () {
        if(this.changePasswordForm.password == '' && this.changePasswordForm.exchangePassword == '')  {
          this.$message.error('至少重置一个密码')
          return
        }
        this.changePasswordConfirmLoading = true
        this.$http({
          url: this.$http.adornUrl('/biz/user/resetpassword'),
          method: 'post',
          data: {
            ...this.changePasswordForm
          }
        }).then(({data}) => {
          if(data && data.code == 0) {
            this.changePasswordConfirmLoading = false
            this.changePasswordVisible = false
            this.$message({
              type: 'success',
              message: '重置成功'
            });
          } else {
            this.$message.error(data.message)
          }
        });
      },
      // 设置状态
      setStatusHandle (userId, status) {
        var name = status == 0 ? '禁用' : '启用';
        this.$confirm('是否要' + name +'该用户', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/biz/user/setstatus'),
            method: 'post',
            data: {
              userId: userId,
              status: status
            }
          }).then(({data}) => {
            this.getDataList()
            this.$message({
              type: 'success',
              message: '操作成功'
            });
          });
        }).catch(() => {
        });
      },
      // 批量设置状态
      setStatusMultiHandle(status) {
         var ids = this.dataListSelections.map(item => {
          return item.userId
        })
        if (!ids){
          this.$message.error('请选择要设置的用户')
          return;
        }
        this.$confirm(`确定对进行批量[${status==0 ? '批量禁用' : '批量启用'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/biz/user/setstatusmulti'),
            method: 'post',
            data: {
              ids: ids,
              status: status
            }
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 显示调账
      showChangeBalance (userId, userBalance) {
        this.changBalanceForm.userBalance = userBalance
        this.changBalanceForm.userId = userId
        this.changBalanceForm.balance = 0
        this.changBalanceForm.status = 0
        this.changeBalanceVisible = true
      },
      // 提交调账
      changeBalanceHandle () {
        if(this.changBalanceForm.balance <= 0) {
          this.$message.error('请输入正确金额')
          return
        }
        this.changBalanceConfirmLoading = true
        this.$http({
          url: this.$http.adornUrl('/biz/user/changebalance'),
          method: 'post',
          data: {
            ...this.changBalanceForm
          }
        }).then(({data}) => {
          this.changBalanceConfirmLoading = false
          this.changeBalanceVisible = false
          if (data.code == 0) {
            this.getDataList()
            this.$message({
              type: 'success',
              message: '操作成功'
            });
          } else {
            this.$message.error(data.message)
          }
        });
      },
      // 修改提现USDT地址
      resetTrcAddressHandle (userId, trcAddress) {
        this.$prompt('请输入USDT地址', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: trcAddress
        }).then(({ value }) => {
          this.$http({
            url: this.$http.adornUrl('/biz/user/updatetrcaddress'),
            method: 'post',
            data: {
              userId: userId,
              trcAddress: value
            }
          }).then(({data}) => {
            if(data.code == 0) {
              this.getDataList()
              this.$message({
                type: 'success',
                message: '操作成功'
              });
            } else {
              this.$message.error(data.message)
            }
          });
        }).catch(() => {
        });
      }

    }
  }
</script>
